package com.octopus.yangj.customized.rule;

import com.octopus.kernal.controller.SearchOperator;
import com.octopus.kernal.model.BaseStruct;
import com.octopus.kernal.service.BusinessRuleInterface;
import com.octopus.yangj.generated.controller.SearchEventCommand;
import com.octopus.yangj.generated.model.EventCategoryStruct;
import com.octopus.yangj.generated.model.EventStruct;
import com.octopus.yangj.generated.service.EventService;
import com.octopus.yangj.util.EventCategoryLevel;
import com.octopus.yangj.util.ServiceFactory;

public class EventCategoryDeleteRule implements BusinessRuleInterface{

	@Override
	public boolean isAllowed(BaseStruct struct) {
		EventCategoryStruct category = (EventCategoryStruct)struct;
		SearchEventCommand searchCommand = new SearchEventCommand();
		searchCommand.setEntity(new EventStruct());
		searchCommand.setOperator(new EventStruct());
		if(EventCategoryLevel.FIRST.getLevel().equals(category.getLevel())){
			searchCommand.getEntity().setFirstCategoryId(category.getId());
			searchCommand.getOperator().setFirstCategoryId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
		}else if(EventCategoryLevel.SECOND.getLevel().equals(category.getLevel())){
			searchCommand.getEntity().setSecondCategoryId(category.getId());
			searchCommand.getOperator().setSecondCategoryId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
		}else if(EventCategoryLevel.THIRD.getLevel().equals(category.getLevel())){
			searchCommand.getEntity().setThirdCategoryId(category.getId());
			searchCommand.getOperator().setThirdCategoryId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
		}
		EventService eventService = ServiceFactory.getEventService();
		int count = eventService.searchCount(searchCommand);
		if(count > 0){
			return false;
		}else{
			return true;
		}
	}

}
